package prototype;

import org.jfree.chart.ChartPanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainApp extends JFrame {

    private JPanel cards;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainApp frame = new MainApp();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainApp() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(400, 400, 750, 600);
        setLocationRelativeTo(this.getRootPane());
        setBackground(Color.white);

        WeeklyUsageGraph weeklyUsage = new WeeklyUsageGraph();
        final ScheduleView scheduleView = new ScheduleView();
        DailyUsageGraph dailyUsage = new DailyUsageGraph();
        final ChartPanel weeklyPanel = weeklyUsage.createGraph();
        final ChartPanel dailyPanel = dailyUsage.createGraph();

        weeklyUsage.getDailyUsage().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dailyButtonPressed();
            }
        });
        weeklyUsage.getScheduleButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scheduleButtonPressed();
            }
        });
        dailyUsage.getScheduleButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scheduleButtonPressed();
            }
        });
        dailyUsage.getBtnWeeklyUsage().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                weeklyButtonPressed();
            }
        });
        scheduleView.getDailyUsage().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dailyButtonPressed();
            }
        });
        scheduleView.getBtnWeeklyUsage().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                weeklyButtonPressed();
            }
        });

        cards = new JPanel(new CardLayout());
        cards.add(weeklyPanel, "Weekly Usage");
        cards.add(scheduleView.getContentPane(), "Schedule");
        cards.add(dailyPanel, "Daily Usage");

        getContentPane().add(cards);
        setVisible(true);
    }

    public void dailyButtonPressed() {
        CardLayout cardLayout = (CardLayout) cards.getLayout();
        cardLayout.show(cards, "Daily Usage");
    }

    public void weeklyButtonPressed() {
        CardLayout cardLayout = (CardLayout) cards.getLayout();
        cardLayout.show(cards, "Weekly Usage");
    }

    public void scheduleButtonPressed() {
        CardLayout cardLayout = (CardLayout) cards.getLayout();
        cardLayout.show(cards, "Schedule");
    }
}
